// Copyright 2021 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package importer

import (
	"context"
	"fmt"
	"os"
	"strings"

	"github.com/GoogleCloudPlatform/healthcare-data-protection-suite/internal/terraform"
	"github.com/ryanuber/columnize"
	"google.golang.org/api/cloudresourcemanager/v1"
)

// ResourceManagerLien defines a struct with the necessary information for a google_resource_manager_lien to be imported.
type ResourceManagerLien struct{}

// ImportID returns the ID of the resource to use in importing.
func (i *ResourceManagerLien) ImportID(rc terraform.ResourceChange, pcv ConfigMap, interactive bool) (string, error) {
	// Can't import if not interactive, since lien names are system-generated and will never be in the plan.
	if !interactive {
		return "", &InsufficientInfoErr{[]string{"name"}, "Lien name is system-generated and will never be in the plan"}
	}

	parentI, err := fromConfigValues("parent", rc.Change.After, pcv)
	if err != nil {
		return "", err
	}
	parentFull := fmt.Sprintf("%s", parentI)

	// Parent is in the form "project/1234567890", but we need to drop the prefix.
	parts := strings.Split(parentFull, "/")
	parent := parts[len(parts)-1]

	// The name is not set, it is autogenerated by the system.
	// Try to find existing liens for this parent to offer as choices.
	liens, err := i.getLiens(parentFull)
	if err != nil {
		return "", err
	}

	if len(liens) <= 0 {
		// There are no liens, just return that it doesn't exist so it can be created.
		return "", &DoesNotExistErr{rc.Address}
	}

	// Present the choices, if any
	// Format the liens more nicely.
	liensLines := []string{"Name|Origin|Reason|Restrictions"}
	for _, lien := range liens {
		// The name is always "liens/<id for import>".
		lienID := strings.Split(lien.Name, "/")[1]
		liensLines = append(liensLines, fmt.Sprintf("%v|%v|%v|%v", lienID, lien.Origin, lien.Reason, strings.Join(lien.Restrictions, ", ")))
	}

	prompt := fmt.Sprintf("Found the following liens in %v:\n%s", parentFull, columnize.SimpleFormat(liensLines))

	// Get the value from the user
	name, err := fromUser(os.Stdin, "name", prompt)
	if err != nil {
		return "", err
	}

	return fmt.Sprintf("%v/%v", parent, name), nil
}

func (i *ResourceManagerLien) getLiens(parent string) (liens []*cloudresourcemanager.Lien, err error) {
	ctx := context.Background()
	service, err := cloudresourcemanager.NewService(ctx)
	if err != nil {
		return liens, err
	}

	liensService := cloudresourcemanager.NewLiensService(service)
	listCall := liensService.List()
	listCall.Parent(parent)

	// Do the call repeatedly, as long as there are more pages.
	for {
		resp, err := listCall.Do()
		if err != nil {
			return liens, err
		}
		liens = append(liens, resp.Liens...)
		if resp.NextPageToken == "" {
			// No more pages, break out.
			break
		}
		listCall.PageToken(resp.NextPageToken)
	}

	return liens, nil
}
